package org.dromara.web.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.web.system.domain.AppDriverDetail;
import org.dromara.web.system.domain.bo.AppDriverDetailBo;
import org.dromara.web.system.domain.vo.AppDriverDetailVo;
import org.dromara.web.system.mapper.AppDriverDetailMapper;
import org.dromara.web.system.service.IAppDriverDetailService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 司机详情Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppDriverDetailServiceImpl implements IAppDriverDetailService {

    private final AppDriverDetailMapper baseMapper;

    /**
     * 查询司机详情
     *
     * @param id 主键
     * @return 司机详情
     */
    @Override
    public AppDriverDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询司机详情列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 司机详情分页列表
     */
    @Override
    public TableDataInfo<AppDriverDetailVo> queryPageList(AppDriverDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppDriverDetail> lqw = buildQueryWrapper(bo);
        Page<AppDriverDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的司机详情列表
     *
     * @param bo 查询条件
     * @return 司机详情列表
     */
    @Override
    public List<AppDriverDetailVo> queryList(AppDriverDetailBo bo) {
        LambdaQueryWrapper<AppDriverDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppDriverDetail> buildQueryWrapper(AppDriverDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppDriverDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppDriverDetail::getId);
        lqw.eq(bo.getUserId() != null, AppDriverDetail::getUserId, bo.getUserId());
        lqw.eq(bo.getIdentityType() != null, AppDriverDetail::getIdentityType, bo.getIdentityType());
        lqw.eq(bo.getStatus() != null, AppDriverDetail::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getTransportCompany()), AppDriverDetail::getTransportCompany, bo.getTransportCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyScale()), AppDriverDetail::getCompanyScale, bo.getCompanyScale());
        lqw.eq(StringUtils.isNotBlank(bo.getTaxpayerId()), AppDriverDetail::getTaxpayerId, bo.getTaxpayerId());
        lqw.like(StringUtils.isNotBlank(bo.getContactName()), AppDriverDetail::getContactName, bo.getContactName());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), AppDriverDetail::getContactPhone, bo.getContactPhone());
        lqw.like(StringUtils.isNotBlank(bo.getDriverName()), AppDriverDetail::getDriverName, bo.getDriverName());
        lqw.eq(StringUtils.isNotBlank(bo.getDriverIdCard()), AppDriverDetail::getDriverIdCard, bo.getDriverIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getDriverPhone()), AppDriverDetail::getDriverPhone, bo.getDriverPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getTruckFrontPhoto()), AppDriverDetail::getTruckFrontPhoto, bo.getTruckFrontPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonalPhoto()), AppDriverDetail::getPersonalPhoto, bo.getPersonalPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getDrivingLicensePhoto()), AppDriverDetail::getDrivingLicensePhoto, bo.getDrivingLicensePhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleLicensePhoto()), AppDriverDetail::getVehicleLicensePhoto, bo.getVehicleLicensePhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getCarPlate()), AppDriverDetail::getCarPlate, bo.getCarPlate());
        lqw.eq(StringUtils.isNotBlank(bo.getCarType()), AppDriverDetail::getCarType, bo.getCarType());
        lqw.eq(bo.getCarLength() != null, AppDriverDetail::getCarLength, bo.getCarLength());
        lqw.eq(bo.getLoadCapacity() != null, AppDriverDetail::getLoadCapacity, bo.getLoadCapacity());
        return lqw;
    }

    /**
     * 新增司机详情
     *
     * @param bo 司机详情
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppDriverDetailBo bo) {
        AppDriverDetail add = MapstructUtils.convert(bo, AppDriverDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改司机详情
     *
     * @param bo 司机详情
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppDriverDetailBo bo) {
        AppDriverDetail update = MapstructUtils.convert(bo, AppDriverDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppDriverDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除司机详情信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
